<?php
/**
  * tframe::DTask.server.php
  *
  *
  * @copyright Copyright (c) 2012 - 2014, huxiaomin
  * @author huxiaomin    2012-5-14  
  * @since		Version 1.0
  *
  */


class DTaskServer{

	static $fatal_error = 1;
	public $task;
	
    function __construct(){
    	$this->task = new DTask();
    	ob_start();
    	$return =  $this->start();
    	$ob = ob_get_clean();
    	$return['ob'] = $ob;
    	echo json_encode($return);
    	return $return;
    }
    
    function start(){
        /**
         * 判断是否结果回传
         * */
        if ($_REQUEST['sys_uri'] === T_RECEIVE_HTTP ){
            return $this->task->receive($_REQUEST);     
        }
        
        /**
         * 判断是否回调
         * */
        if ($_REQUEST['sys_uri'] === T_CALLBACK_HTTP ){
        	$id = $_REQUEST['task_id'];
        	$task = $this->task->getTask($id);
        	$task['response'] = $_REQUEST['request'];
        	return $this->taskCallBack($task);
        }
        
        /**
         * 判断是否执行
         */
        if ($_REQUEST['sys_uri'] === T_EXEC_HTTP ){
            $data = $_REQUEST['request'];
            $json = json_decode($data, 1);
            if (empty($json['class']) || empty($json['api'])){
                return $this->task->out(0,'','error request');
            }
            return $this->taskExec($json['class'], $json['api'], $json['data']);
        }
        
        /**
         * 接收一般请求
         */
        $class = $_REQUEST['sys_route']['class'];
        $api = $_REQUEST['sys_route']['api'];
        $data = $_REQUEST['request'];
        $res = json_encode(array('class'=>$class,'api'=>$api, 'data'=>$data));
        //print_r($res);
        $input = $this->task->in('exec',$res,0,0);
        /*只容许60秒*/
        $return_call = TCurl::post(LOCAL_HOST.T_EXEC_HTTP,$input,60);
        if ($return_call['success'] == 1){
            $this->request($return_call['data']);
            //file_put_contents('thread.log.html',print_r($_REQUEST,1));
            return $this->task->out(1,$return_call['data']);
        }
        return $this->task->out(0,$return_call['data'],$return_call['error']);
    }
    
    
    /**
     * 如果请求中带有receive_async则调用回调方法
     * @param $return
     */
    function request($return){
        if ($_REQUEST['receive_async'] && $_REQUEST['task_id']){
            $task = array(
                       'request'=>$_REQUEST['task_id'],
                       'type' => 'response',
                       'uri' => $_REQUEST['receive_async'],
                       'response'=>$return,
                    );
            $this->task->startResponse($task, 5);
        }      
    }
    
    /*回调方法*/
    public function taskCallBack($task){
        $callback = $task['callback'];
        $response = $task['response'];
        $call_back = explode('::',$callback);
        $class = $call_back[0].'CallBack';
        $dir_name = dirname(__FILE__).'/callback/'.$call_back[0].'.callback.php';
        if (!file_exists($dir_name)){
           return $this->task->out(0,'','没有此文件'.$dir_name);
        }
        require_once $dir_name; 
        $class = new $class();
        if (is_callable(array($class,$call_back[1]))){
            return call_user_func(array($class,$call_back[1]),$response);
        }else {
            return $this->task->out(0,'','没有此功能');
        }
    }
    
    /*执行方法*/
    public function taskExec($class, $api, $response){
        $dir_name = dirname(__FILE__).'/server/'.$class.'.server.php';
        if (!file_exists($dir_name)){
           return $this->task->out(0,'','没有此文件');
        }
        require_once $dir_name; 
        $class .= 'Server';
        $class = new $class();
        if (is_callable(array($class,$api))){
            $return = call_user_func(array($class,$api),$response);
	        if (is_array($return)){
	                $return = json_encode($return);
	        }
	        return $this->task->out(1,$return);
        }else {
            return $this->task->out(0,'','没有此功能');
        }
    }
    
}